//
//  CustomProjectSmallContentView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/21.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "CustomProjectSmallContentView.h"

//
#import <OCSkeleton.h>

#import "BCTimerHandler.h"

#import "NSDate+BCustom.h"

static CGFloat CustomProjectSmallContentViewTopPadding = 17.0;
static CGFloat CustomProjectSmallContentViewBottomPadding = 12.0;
static CGFloat CustomProjectSmallContentViewLeftRightPadding = 20.0;


@interface CustomProjectSmallContentView () <OCGradientsOwner,BCTimerHandlerDelegate>

#pragma mark - ContainerViews
/**
 *titleContainerView
 */
@property (nonatomic, strong) OCGradientContainerView *titleContainerView;
/**
 *displayCollectionContainerView
 */
@property (nonatomic, strong) OCGradientContainerView *displayCollectionContainerView;
@property (nonatomic, strong) OCGradientContainerView *rateTitleContainerView;
@property (nonatomic, strong) OCGradientContainerView *rateDataContainerView;
@property (nonatomic, strong) OCGradientContainerView *lastTitleContainerView;
@property (nonatomic, strong) OCGradientContainerView *lastDataContainerView;

/**
 *gradientLayers
 */
@property (nonatomic, strong) NSMutableArray *oc_gradientLayers;
/**
 *skeletonViews
 */
@property (nonatomic, strong) NSMutableArray *oc_skeletonViews;



/**
 *BCTimerHandler 预购产业倒计时需要
 */
@property (nonatomic, strong) BCTimerHandler *timerHandler;

@end

@implementation CustomProjectSmallContentView

- (BCTimerHandler *)timerHandler {
    if (!_timerHandler) {
        _timerHandler = [BCTimerHandler new];
        _timerHandler.delegate = self;
    }
    return _timerHandler;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}

- (void)setupSubviews {
    self.titleLabel = [UILabel new];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.titleLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(CustomProjectSmallContentViewTopPadding);
        make.left.mas_equalTo(self).offset(CustomProjectSmallContentViewLeftRightPadding);
        make.right.mas_equalTo(self).offset(-CustomProjectSmallContentViewLeftRightPadding);
        make.height.mas_equalTo(self.titleLabel.font.lineHeight+1);
    }];
    self.displayCollection = [[HomeCellDisplayCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[UICollectionViewFlowLayout new]];
    [self addSubview:self.displayCollection];
    [self.displayCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel.mas_left);
        make.right.mas_equalTo(self).offset(-CustomProjectSmallContentViewLeftRightPadding);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5.0);
        make.height.mas_equalTo([HomeCellDisplayCollectionView defaultHeight]);
    }];
    //
    self.rateView = [HomeCellDataView new];
    self.rateView.dataLabel.textColor = [UIColor darkMainColor];
    [self addSubview:self.rateView];
    [self.rateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(CustomProjectSmallContentViewLeftRightPadding);
        make.right.mas_equalTo(self.mas_centerX).offset(-CustomProjectSmallContentViewLeftRightPadding/2);
        make.bottom.mas_equalTo(self).offset(-CustomProjectSmallContentViewBottomPadding);
        make.height.mas_equalTo([HomeCellDataView defaultHeight]);
    }];
    self.lastView = [HomeCellDataView new];
    [self addSubview:self.lastView];
    [self.lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_centerX).offset(CustomProjectSmallContentViewLeftRightPadding/2);
        make.right.mas_equalTo(self).offset(-CustomProjectSmallContentViewLeftRightPadding);
        make.bottom.mas_equalTo(self.rateView);
        make.height.mas_equalTo([HomeCellDataView defaultHeight]);
    }];
}

- (void)setContainerViews {
    self.titleContainerView = [OCGradientContainerView new];
    [self addSubview:self.titleContainerView];
    [self.titleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.centerY.mas_equalTo(self.titleLabel);
        make.height.mas_equalTo(self.titleLabel.font.lineHeight);
        make.right.mas_equalTo(self).offset(-CustomProjectSmallContentViewLeftRightPadding);
    }];
    [self.oc_skeletonViews addObject:self.titleContainerView];
    [self.oc_gradientLayers addObject:self.titleContainerView.gradientLayer];
    //
    self.displayCollectionContainerView = [OCGradientContainerView new];
    [self addSubview:self.displayCollectionContainerView];
    [self.displayCollectionContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.displayCollection);
        make.centerY.mas_equalTo(self.displayCollection);
        make.height.mas_equalTo(self.displayCollection);
        make.width.mas_equalTo(150.0);
    }];
    [self.oc_skeletonViews addObject:self.displayCollectionContainerView];
    [self.oc_gradientLayers addObject:self.displayCollectionContainerView.gradientLayer];
    //
    self.rateTitleContainerView = [OCGradientContainerView new];
    [self addSubview:self.rateTitleContainerView];
    [self.rateTitleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rateView.titleLabel);
        make.centerY.mas_equalTo(self.rateView.titleLabel);
        make.height.mas_equalTo(self.rateView.titleLabel);
        make.width.mas_equalTo(100.0);
    }];
    [self.oc_skeletonViews addObject:self.rateTitleContainerView];
    [self.oc_gradientLayers addObject:self.rateTitleContainerView.gradientLayer];
    //
    self.rateDataContainerView = [OCGradientContainerView new];
    [self addSubview:self.rateDataContainerView];
    [self.rateDataContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rateView.dataLabel);
        make.centerY.mas_equalTo(self.rateView.dataLabel);
        make.height.mas_equalTo(self.rateView.dataLabel);
        make.width.mas_equalTo(120.0);
    }];
    [self.oc_skeletonViews addObject:self.rateDataContainerView];
    [self.oc_gradientLayers addObject:self.rateDataContainerView.gradientLayer];
    //
    self.lastTitleContainerView = [OCGradientContainerView new];
    [self addSubview:self.lastTitleContainerView];
    [self.lastTitleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lastView.titleLabel);
        make.centerY.mas_equalTo(self.lastView.titleLabel);
        make.height.mas_equalTo(self.lastView.titleLabel);
        make.width.mas_equalTo(100.0);
    }];
    [self.oc_skeletonViews addObject:self.lastTitleContainerView];
    [self.oc_gradientLayers addObject:self.lastTitleContainerView.gradientLayer];
    //
    self.lastDataContainerView = [OCGradientContainerView new];
    [self addSubview:self.lastDataContainerView];
    [self.lastDataContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lastView.dataLabel);
        make.centerY.mas_equalTo(self.lastView.dataLabel);
        make.height.mas_equalTo(self.lastView.dataLabel);
        make.width.mas_equalTo(120.0);
    }];
    [self.oc_skeletonViews addObject:self.lastDataContainerView];
    [self.oc_gradientLayers addObject:self.lastDataContainerView.gradientLayer];
}

- (NSMutableArray *)oc_gradientLayers {
    if (!_oc_gradientLayers) {
        _oc_gradientLayers = [NSMutableArray array];
    }
    return _oc_gradientLayers;
}
- (NSMutableArray *)oc_skeletonViews {
    if (!_oc_skeletonViews) {
        _oc_skeletonViews = [NSMutableArray array];
    }
    return _oc_skeletonViews;
}
#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    return  self.oc_gradientLayers;
}
- (NSArray <UIView *>*)skeletonViews {
    return self.oc_skeletonViews;
}

- (void)setContentData:(BCProjectModel *)contentData {
    _contentData = contentData;
    //
    if (_timerHandler) {
        [_timerHandler stop];
    }
    //
    if (contentData) {
        [self clearContainerViews];
        //
        self.titleLabel.text = contentData.project_name;
        //
        self.rateView.title = contentData.repayCaption;
        self.rateView.data = contentData.repay;
        
        
        if (contentData.advance_taking.length) {//预购的显示
            self.lastView.title = contentData.advance_taking;
            [self timerHandlerRepeatsAction];
            [self.timerHandler start];
        }else {
            self.lastView.title = contentData.surplus_title;
            self.lastView.data = contentData.surplus;
        }
        
        self.displayCollection.displayArray = [contentData.tags componentsSeparatedByString:@","];
    }else {
        self.titleLabel.text = nil;
        self.rateView.title = nil;
        self.rateView.data = nil;
        self.lastView.title = nil;
        self.lastView.data = nil;
        
        [self showContainerViews];
    }
}

#pragma mark - BCTimerHandlerDelegate
- (void)timerHandlerRepeatsAction {
    if (!self.contentData.advance_taking.length) {//添加把耦合
        return;
    }
    
    NSDate *date = [BCServiceTimestampManager shared].date;
    NSDate *endDate = [NSDate stringToDate:self.contentData.end_time format:FullDateFormateString];
    self.lastView.data = [NSDate countdownStringBetweenDate:date endDate:endDate];
}




- (void)clearContainerViews {
    for (OCGradientContainerView *view in self.oc_skeletonViews) {
        view.backgroundColor = [UIColor clearColor];
        view.alpha = 0;
    }
}
- (void)showContainerViews {
    for (OCGradientContainerView *view in self.oc_skeletonViews) {
        view.backgroundColor = [OCGradientContainerView appearance].backgroundColor;
        view.alpha = 1;
    }
}


- (void)dealloc {
    if (_timerHandler) {
        [_timerHandler stop];
    }
}

@end
